package cn.com.signcomplex.musicplayer.fragments;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import ca.uol.aig.fftpack.RealDoubleFFT;
import cn.com.signcomplex.musicplayer.AudioFxActivity;
import cn.com.signcomplex.musicplayer.R;

/**
 * Created by architjn on 27/11/15.
 */
public class AudioFFTFragment extends Fragment {

    private Context context;
    private View mainView;
    private Button startBtn;

    private ImageView imageView;
    private int frequency = 8000;
    private int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
    private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
    private RealDoubleFFT transformer;
    private int blockSize = 256;
    private boolean started = false;
    private Canvas canvas;
    private Paint paint;
    private Bitmap bitmap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_audio_fft,
                container, false);
        context = view.getContext();
        mainView = view;
        init();
        return view;
    }

    private void init() {
        startBtn = (Button) mainView.findViewById(R.id.start_button);
        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                started=true;
//                new RecordAudio().execute();
                startActivity(new Intent(context, AudioFxActivity.class));
//                startActivity(new Intent(context, SpectrumActivity.class));
//                startActivity(new Intent(context, WaveInAppActivity.class));
            }
        });

        imageView = (ImageView) mainView.findViewById(R.id.fft_imageview);
        transformer = new RealDoubleFFT(blockSize);
        bitmap = Bitmap.createBitmap(256, 100, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        paint = new Paint();
        paint.setColor(Color.GREEN);
        imageView.setImageBitmap(bitmap);
    }

    private class RecordAudio extends AsyncTask<Void, double[], Void> {
        @Override
        protected Void doInBackground(Void... params) {
            int bufferSize = AudioRecord.getMinBufferSize(frequency,
                    channelConfiguration, audioEncoding);
            AudioRecord audioRecord = new AudioRecord(
                    MediaRecorder.AudioSource.MIC, frequency,
                    channelConfiguration, audioEncoding, bufferSize);
            short[] buffer = new short[blockSize];
            double[] toTransform = new double[blockSize];
            audioRecord.startRecording();
            while (started) {
                //将record的数据 读到buffer中，但是我认为叫做write可能会比较合适些。
                int bufferResult = audioRecord.read(buffer, 0, blockSize);
                for (int i = 0; i < bufferResult; i++) {
                    toTransform[i] = (double) buffer[i] / Short.MAX_VALUE;
                }
                transformer.ft(toTransform);
                publishProgress(toTransform);
            }
            audioRecord.stop();
            return null;
        }
        @Override
        protected void onProgressUpdate(double[]... values) {
            super.onProgressUpdate(values);
            canvas.drawColor(Color.BLACK);
            for (int i = 0; i < values[0].length; i++) {
                int x=i;
                int downy=(int)(100-(values[0][i])*10);
                int upy=100;
                canvas.drawLine(x, downy, x, upy, paint);
            }
            imageView.invalidate();
        }
    }


}
